package com.taotao.manage.controller;

import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    /**
     * 查询一个商品
     */
    @RequestMapping("/{itemId}")
    public Item getItemById(@PathVariable Long itemId) {
        return itemService.queryById(itemId);
    }

    /**
     * 查询商品列表
     */
    /*@RequestMapping(method = RequestMethod.GET)
    public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
        List<Item> list = itemService.queryListInPage(page, rows);
        PageInfo<Item> pageInfo = new PageInfo<>(list);
        return new EasyUIDataGridResult(pageInfo.getTotal(),pageInfo.getRows);
    }*/
    /**
     * 按条件查询商品列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public EasyUIDataGridResult getItemList(@RequestParam(value = "title",required = false)String title, Integer page, Integer rows) {
        return itemService.queryItemList(title, page, rows);
    }
    /**
     * 保存商品
     */
    @RequestMapping(method = RequestMethod.POST)
    public void saveItem(Item item, @RequestParam(value = "desc", required = false) String desc) {
        itemService.saveItem(item,desc);
    }
    /**
     * 修改商品
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateItem(Item item, @RequestParam(value = "desc", required = false) String desc) {
        itemService.updateItem(item,desc);
    }
    /**
     * 删除商品
     */
    @RequestMapping(value = "/delete")
    public void deleteItem(Long[] ids) {
        Item item=new Item();
        item.setStatus(3);
        itemService.updateSelectiveByIds(item,ids);
    }
    /**
     * 下架商品
     */
    @RequestMapping(value = "/instock")
    public void instockItem(Long[] ids) {
        Item item=new Item();
        item.setStatus(2);
        itemService.updateSelectiveByIds(item,ids);
    }
    /**
     * 上架商品
     */
    @RequestMapping(value = "/reshelf")
    public void reshelfItem(Long[] ids) {
        Item item=new Item();
        item.setStatus(1);
        itemService.updateSelectiveByIds(item,ids);
    }
}
